<?php

declare(strict_types=1);

namespace App\Admin\Controller;

use App\Admin\Annotation\Auth;
use App\Admin\Request\System\User\SystemUserLoginRequest;
use App\Admin\Service\System\SystemUserService;
use App\Admin\Service\System\UserAuthService;
use DI\Attribute\Inject;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\Annotation\Attributes\Route\RequestMapping;
use Newbie\Vo\UserServiceVo;
use support\Response;


/**
 * Class LoginController
 * @package App\system\Controller
 */
class LoginController extends BaseController {
    #[Inject]
    protected SystemUserService $systemUserService;

    #[Inject]
    protected UserAuthService $userService;

    /**
     * @return Response
     */
    #[GetMapping("/captcha")]
    public function captcha (): Response {
        return $this->success($this->systemUserService->getCaptcha());
    }

    /**
     * @param SystemUserLoginRequest $request
     * @return Response
     */
    #[PostMapping("/login")]
    public function login (SystemUserLoginRequest $request): Response {
        $requestData = $request->validated();
        $vo          = new UserServiceVo();
        $vo->setUsername($requestData['username']);
        $vo->setPassword($requestData['password']);
        $vo->setVerifyCode($requestData['code']);
        $vo->setVerifyKey($requestData['key']);
        return $this->success(['token' => $this->userService->login($vo)]);
    }

    /**
     * @return Response
     */
    #[RequestMapping("/logout"), Auth]
    public function logout (): Response {
        $this->userService->logout();
        return $this->success();
    }

}